{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -q -q llama-index\n",
    "!pip install -U -q deepeval"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from getpass import getpass\n",
    "import openai\n",
    "import nest_asyncio\n",
    "nest_asyncio.apply() \n",
    "\n",
    "if not (openai_api_key := os.getenv(\"OPENAI_API_KEY\")):\n",
    "    openai_api_key = getpass(\"🔑 Enter your OpenAI API key: \")\n",
    "openai.api_key = openai_api_key\n",
    "os.environ[\"OPENAI_API_KEY\"] = openai_api_key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core import VectorStoreIndex, SimpleDirectoryReader\n",
    "\n",
    "# Read LlamaIndex's quickstart on more details, you will need to store your data in \"YOUR_DATA_DIRECTORY\" beforehand\n",
    "documents = SimpleDirectoryReader(\"../data\").load_data()\n",
    "index = VectorStoreIndex.from_documents(documents)\n",
    "rag_application = index.as_query_engine()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from deepeval.integrations.llama_index import DeepEvalFaithfulnessEvaluator\n",
    "\n",
    "# An example input to your RAG application\n",
    "user_input = \"What is LlamaIndex?\"\n",
    "\n",
    "# LlamaIndex returns a response object that contains\n",
    "# both the output string and retrieved nodes\n",
    "response_object = rag_application.query(user_input)\n",
    "\n",
    "evaluator = DeepEvalFaithfulnessEvaluator()\n",
    "evaluation_result = evaluator.evaluate_response(\n",
    "    query=user_input, response=response_object\n",
    ")\n",
    "print(evaluation_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from deepeval.integrations.llama_index import (\n",
    "    DeepEvalAnswerRelevancyEvaluator,\n",
    "    DeepEvalFaithfulnessEvaluator,\n",
    "    DeepEvalContextualRelevancyEvaluator,\n",
    "    DeepEvalSummarizationEvaluator,\n",
    "    DeepEvalBiasEvaluator,\n",
    "    DeepEvalToxicityEvaluator,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "evaluator = DeepEvalAnswerRelevancyEvaluator()\n",
    "evaluation_result = evaluator.evaluate_response(\n",
    "    query=user_input, response=response_object\n",
    ")\n",
    "print(evaluation_result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "build-venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
